package com.sac.approval.controller;

import com.sac.approval.domain.ApprovalLineTemplateDO;
import com.sac.approval.domain.ApprovalTemplateDO;
import com.sac.approval.domain.AttachmentDO;
import com.sac.approval.service.ApprovalQueryService;
import com.sac.approval.service.ApprovalTemplateService;
import com.sac.approval.vo.ApItemVO;
import com.sac.common.annotation.AuthToken;
import com.sac.common.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 审批邮件模板
 * @author xiaodong
 * @date 2018年9月17日 上午10:51:29
 * @desc
 */
@RestController
@RequestMapping("/approval")
public class TemplateController {

    @Autowired
    private ApprovalTemplateService approvalTemplateService;

    @Autowired
    private ApprovalQueryService approvalQueryService;

    /**
	 * 保存草稿箱
	 * @param apItemVO
	 * @return
	 */
	@AuthToken
	@PostMapping("/savePostponed")
	public Object savePostponed(ApItemVO apItemVO) {
		//master
		ApprovalTemplateDO app = new ApprovalTemplateDO();
		app.setTitle(apItemVO.getApTtl());
		app.setContent(apItemVO.getTxtClobcn());
		app.setContType(apItemVO.getTxtTypeCd());
		app.setCharSet(apItemVO.getChsetCd());
		app.setSecretType(apItemVO.getApDocSecuTypeCd());
		app.setNotifyType(apItemVO.getNtfTypeCd());
		app.setUrgentType(apItemVO.getUrgYn());
		app.setDot("D");
		app.setCreateTime(new Date());
		app.setCreateUser(ShiroUtils.getEmpId().toString());
		//审批line
		List<ApprovalLineTemplateDO> appLines = new ArrayList<>();
		if(apItemVO.getAapTaplnVOList()!=null){
			Arrays.asList(apItemVO.getAapTaplnVOList()).forEach(receiver -> {

				ApprovalLineTemplateDO appLine = new ApprovalLineTemplateDO();
				appLine.setHandleType(Integer.parseInt(receiver.getApRlCd()));
				appLine.setReceiverEpid(receiver.getApRelrEpid());
				appLine.setReceiverMail(receiver.getAprelrmailaddr());
				appLine.setSeq(Integer.parseInt(receiver.getApSeq()));
				appLine.setArbPmtYn(receiver.getArbPmtYn());
				appLine.setTxtMdfyPmtYn(receiver.getTxtMdfyPmtYn());
				appLine.setPathMdfyPmtYn(receiver.getPathMdfyPmtYn());

				appLines.add(appLine);
			});
		}

		//附件
		List<AttachmentDO> attachments = new ArrayList<AttachmentDO>();
		if(apItemVO.getFiles()!=null) {
			Arrays.asList(apItemVO.getFiles()).forEach(file -> {
				String fileName = FileUtil.renameToUUID(file.getOriginalFilename());

				AttachmentDO attachment = new AttachmentDO();
				attachment.setFileName(fileName);
				attachment.setFileOriginName(file.getOriginalFilename());
				attachment.setFileSize(file.getSize());// kb
				attachment.setFileType(file.getContentType());

				attachments.add(attachment);
				try {
					// 上传附件
					SftpUtil.uploadFile(file.getBytes(),fileName);
				} catch (Exception e) {
					e.printStackTrace();
					throw new BDException("文件上传失败");
				}
			});
		}

		//保存信息
		approvalTemplateService.saveApprovalTemplate(app, appLines, attachments);

		return R.ok();
	}

	/**
	 *
	 *
	 * 更新草稿箱
	 * @param apItemVO
	 * @return
	 */
	@AuthToken
	@PostMapping("/updatePostponed")
	public Object updatePostponed(ApItemVO apItemVO) {
		//master
		ApprovalTemplateDO app = approvalQueryService.getApprovalTemplate(apItemVO.getApInfId());
		app.setTitle(apItemVO.getApTtl());
		app.setContent(apItemVO.getTxtClobcn());
		app.setContType(apItemVO.getTxtTypeCd());
		app.setCharSet(apItemVO.getChsetCd());
		app.setSecretType(apItemVO.getApDocSecuTypeCd());
		app.setNotifyType(apItemVO.getNtfTypeCd());
		app.setUrgentType(apItemVO.getUrgYn());
		app.setUpdateTime(new Date());
		//审批line
		List<ApprovalLineTemplateDO> appLines = new ArrayList<>();
		if(apItemVO.getAapTaplnVOList()!=null){
			Arrays.asList(apItemVO.getAapTaplnVOList()).forEach(receiver -> {

				ApprovalLineTemplateDO appLine = new ApprovalLineTemplateDO();
				appLine.setHandleType(Integer.parseInt(receiver.getApRlCd()));
				appLine.setReceiverEpid(receiver.getApRelrEpid());
				appLine.setReceiverMail(receiver.getAprelrmailaddr());
				appLine.setSeq(Integer.parseInt(receiver.getApSeq()));
				appLine.setArbPmtYn(receiver.getArbPmtYn());
				appLine.setTxtMdfyPmtYn(receiver.getTxtMdfyPmtYn());
				appLine.setPathMdfyPmtYn(receiver.getPathMdfyPmtYn());

				appLines.add(appLine);
			});
		}

		//附件
		List<AttachmentDO> attachments = new ArrayList<AttachmentDO>();
		if(apItemVO.getFiles()!=null) {
			Arrays.asList(apItemVO.getFiles()).forEach(file -> {
				String fileName = FileUtil.renameToUUID(file.getOriginalFilename().split("\\?")[0]);

				AttachmentDO attachment = new AttachmentDO();
				attachment.setFileName(fileName);
				attachment.setFileOriginName(file.getOriginalFilename().split("\\?")[0]);
				if(file.getSize() == 0L){
					attachment.setFileSize(Long.parseLong(file.getOriginalFilename().split("\\?")[1]));
				}else {
					attachment.setFileSize(file.getSize());// B
				}
				attachment.setFileType(file.getContentType());

				attachments.add(attachment);
				try {
					// 上传附件
					SftpUtil.uploadFile(file.getBytes(),fileName);
				} catch (Exception e) {
					e.printStackTrace();
					throw new BDException("文件上传失败");
				}
			});
		}

		//保存信息
		approvalTemplateService.updateApprovalTemplate(app, appLines, attachments);

		return R.ok();
	}

	/**
	 * 批量删除审批
	 *
	 * @param appTemplateIds
	 * @return
	 */
	@AuthToken
	@PostMapping("/batchDeletePostponed")
	public Object batchDelete(@RequestParam String[] appTemplateIds) {
		List<ApprovalTemplateDO> approvalTemplateList = new ArrayList<>();
		Arrays.asList(appTemplateIds).forEach(appId->{
			ApprovalTemplateDO approvalTemplate = approvalQueryService.getApprovalTemplate(appId);
			approvalTemplate.setIsDelete("Y");
			approvalTemplateList.add(approvalTemplate);
		});

		approvalTemplateService.batchUpdate(approvalTemplateList);
		return R.ok();
	}

}
